﻿Public Class MIXListMaker

	Private Sub BuildListButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BuildListButton.Click
		EnableUI(False)

		Dim html As String = ""
		html = BuildListAgilityPack(CatalogTextBox.Text)

		WebBrowser1.DocumentText = html
		CurrentSessionLabel.Text = ""

		EnableUI(True)

	End Sub

	Private Sub EnableUI(ByVal enable As Boolean)
		CatalogTextBox.Enabled = enable

		BuildListButton.Enabled = enable

		VerifyLinkCheckBox.Enabled = enable
		WMVCheckBox.Enabled = enable
		WMVHQCheckBox.Enabled = enable
		ZuneCheckBox.Enabled = enable
		MP4CheckBox.Enabled = enable
		ZIPCheckBox.Enabled = enable
		PPTXCheckBox.Enabled = enable
		CopyLinkLabel.Enabled = enable

	End Sub

	Private Function BuildListAgilityPack(ByVal CatalogURL As String) As String

		Dim htmWeb As New HtmlAgilityPack.HtmlWeb

		Dim htmDoc As HtmlAgilityPack.HtmlDocument = htmWeb.Load(CatalogURL)

		Dim links As HtmlAgilityPack.HtmlNodeCollection = htmDoc.DocumentNode.SelectNodes("//div[@class='summary']")

		Dim ls = New Generic.List(Of Session)

		For Each link As HtmlAgilityPack.HtmlNode In links
			Dim title = link.ChildNodes(0).InnerText.Trim
			Dim code = link.ChildNodes(1).InnerText.Replace("MIX09-", "")

			If code.Trim.Length = 0 AndAlso link.ChildNodes.Count = 3 Then
				code = link.ChildNodes(2).InnerText.Replace("MIX09-", "")
			End If

			If title.Trim.Length = 0 Then
				title = "Title Unknown " & code
			End If

			Debug.Assert(code.Trim.Length > 0)
			Debug.Assert(title.Trim.Length > 0)


			ls.Add(New Session() With {.Code = code, .Title = title})

		Next
		Return BuildHTML(ls)

	End Function

	Private Function BuildHTML(ByVal ls As Generic.List(Of Session)) As String
		Dim q = From link In ls Order By link.SortTitle

		Dim sb = New System.Text.StringBuilder
		sb.Append("<html><body>")
		sb.Append("<table>")
		sb.Append(GetHeader())

		For Each link As Session In q
			sb.Append("<tr>")

			CurrentSessionLabel.Text = link.Code
			CurrentSessionLabel.Refresh()

			If Not link.Code.Contains("-R") Then
				sb.Append("<td>")
				sb.Append(GetTitle(link.SortTitle, link.Code, VerifyLinkCheckBox.Checked))

				sb.Append("</td>")
				sb.AppendLine(GetVideoLinks(link.Code, VerifyLinkCheckBox.Checked))
			End If

			sb.Append("</tr>")

		Next
		sb.Append("</table>")

		sb.Append("</body></html>")


		Return sb.ToString

	End Function

	Private Sub CopyLinkLabel_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles CopyLinkLabel.LinkClicked
		If WebBrowser1.DocumentText.Length > 0 Then
			My.Computer.Clipboard.SetText(WebBrowser1.DocumentText.Replace("<html><body>", "").Replace("</body></html>", ""))
		End If

	End Sub
	Private Function PageExists(ByVal URL As String, ByVal verifyLink As Boolean) As Boolean

		Return PageVerify.PageExists(URL, verifyLink)

	End Function

	Private Function GetTitle(ByVal title As String, ByVal code As String, ByVal verifyLink As Boolean) As String
		'http://sessions.visitmix.com/MIX09/05W
		Dim sb As New System.Text.StringBuilder

		Dim url = "http://sessions.visitmix.com/MIX09/" & code

		If PageExists(url, False) Then
			sb.Append("<a href=")
			sb.Append(Chr(34))
			sb.Append(url)
			sb.Append(Chr(34))
			sb.Append(">")
			sb.Append(title)
			sb.Append("</a>")
		Else
			sb.Append(title)
		End If

		Return sb.ToString

	End Function

	Private Function GetVideoLinks(ByVal code As String, ByVal verifyLink As Boolean) As String

		Const URL_Prefix As String = "http://mschannel9.vo.msecnd.net/o9/mix/09/"
		Dim sb = New System.Text.StringBuilder
		'http://mschannel9.vo.msecnd.net/o9/mix/09/wmv/05w.wmv
		If WMVHQCheckBox.Checked Then
			sb.AppendLine(GetResourceLink(URL_Prefix & "wmv-hq/", code, "wmv", "WMV-HQ", verifyLink))
		End If
		If WMVCheckBox.Checked Then
			sb.AppendLine(GetResourceLink(URL_Prefix & "wmv/", code, "wmv", "WMV", verifyLink))
		End If
		If ZuneCheckBox.Checked Then
			sb.AppendLine(GetResourceLink(URL_Prefix & "zune/", code, "wmv", "Zune", verifyLink))
		End If
		If MP4CheckBox.Checked Then
			sb.AppendLine(GetResourceLink(URL_Prefix & "mp4/", code, "mp4", "MP4", verifyLink))
		End If
		If ZIPCheckBox.Checked Then
			sb.AppendLine(GetResourceLink(URL_Prefix & "zip/", code, "zip", "ZIP", verifyLink))
		End If
		If PPTXCheckBox.Checked Then
			sb.AppendLine(GetResourceLink(URL_Prefix & "pptx/", code, "pptx", "PPTX", verifyLink))
		End If

		Return sb.ToString
	End Function

	Private Function GetHeader() As String
		Dim sb = New System.Text.StringBuilder

		sb.Append("<thead>")
		sb.Append("<tr>")
		sb.Append("<th width=0>")
		sb.Append("Title")
		sb.Append("</th>")
		If WMVHQCheckBox.Checked Then
			sb.Append("<th width=75>")
			sb.Append("WMV-HQ")
			sb.Append("</th>")
		End If
		If WMVCheckBox.Checked Then
			sb.Append("<th>")
			sb.Append("WMV")
			sb.Append("</th>")
		End If
		If ZuneCheckBox.Checked Then
			sb.Append("<th>")
			sb.Append("Zune")
			sb.Append("</th>")
		End If
		If MP4CheckBox.Checked Then
			sb.Append("<th>")
			sb.Append("MP4")
			sb.Append("</th>")
		End If
		If ZIPCheckBox.Checked Then
			sb.Append("<th>")
			sb.Append("ZIP")
			sb.Append("</th>")
		End If
		If PPTXCheckBox.Checked Then
			sb.Append("<th>")
			sb.Append("PPTX")
			sb.Append("</th>")
		End If


		sb.Append("</tr>")
		sb.Append("</thead>")

		Return sb.ToString

	End Function

	Private Function GetResourceLink(ByVal URLPrefix As String, ByVal code As String, ByVal extension As String, ByVal name As String, ByVal verifyLink As Boolean) As String
		Dim sb = New System.Text.StringBuilder
		Dim url = URLPrefix & code & "." & extension
		Dim url2 = URLPrefix & code.ToLower & "." & extension.ToLower
		Dim finalurl As String = ""

		Dim addlink As Boolean

		If PageExists(url, verifyLink) Then
			addlink = True
			finalurl = url
		Else
			addlink = False
		End If

		If addlink = False AndAlso PageExists(url2.ToLower, verifyLink) Then
			addlink = True
			finalurl = url2
		End If

		sb.Append("<td>")

		If addlink Then
			sb.Append("<a href=")
			sb.Append(Chr(34))
			sb.Append(finalurl)
			sb.Append(Chr(34))
			sb.Append(">")
			sb.Append(name)
			sb.Append("</a>")

		Else
			sb.Append("N/A")
		End If
		sb.Append("</td>")

		Return sb.ToString


	End Function

End Class